home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / VideoToolbox / VideoToolboxSources / GDOpenWindow.c < prev    next >
Encoding:
Text File  |  1993-05-22  |  7.7 KB  |  194 lines  |  [TEXT/KAHL]

  1. /* GDOpenWindow.c
  2. Copyright © 1989-1993 Denis G. Pelli
  3.  
  4. SUMMARY:
  5.  
  6. AddExplicitPalette() adds a palette to a color window or GWorld with all the
  7. colors marked as pmExplicit. This allows you to use PmForeColor() and
  8. PmBackColor() to specify the value you want poked into each pixel by QuickDraw
  9. operations, e.g. EraseRect() and DrawString().
  10.  
  11. RemovePalette() disposes of the palette created by AddExplicitPalette.
  12.  
  13. GDOpenWindow1 opens a full-screen window on the specified screen, and calls
  14. AddExplicitPalette. The window truly fills the screen: any rounded corners are
  15. made square and if the window is on the main screen then the Menu Bar is
  16. hidden.
  17.  
  18. GDDisposeWindow1() closes and discards a color window, disposing of any palette
  19. or color table, generally undoing whatever GDOpenWindow1 did.
  20.  
  21. These routines return and accept a WindowPtr (even though it's actually
  22. a color window), which seems to be the standard way of doing things. 
  23. The older routines, without the "1" suffix, return and accept a CWindowPtr,
  24. which ends up forcing you to do a lot of tedious casting, e.g. when you call
  25. SetPort(), which wants a WindowPtr.
  26.  
  27. COMMENTS:
  28.  
  29. QuickDraw likes to pick a bunch of good colors and stuff them in the clut, in
  30. essentially random order, except that white is first and black is last. It wants
  31. you to specify any desired color as an RGB triplet and then it picks the mysterious
  32. clut index that would provide the closest match. If you're processing grayscale
  33. images, then QuickDraw's approach involves a lot of overhead involving inverse
  34. color tables, and makes the numbers stored in your pixels meaningless (unless
  35. you look them up in the associated color table or palette).
  36.  
  37. The philosophy of the VideoToolbox is to bypass QuickDraw's color model, and
  38. work explicitly with the numbers that are stored in your pixels.
  39. SetPixelsQuickly will efficiently poke (or peek) numbers in your PixMap.
  40. However, if you want to use QuickDraw's drawing operations, especially
  41. EraseRect() and DrawString() then you need a way to specify the foreground and
  42. background colors. AddExplicitPalette() gives your window a palette in which all
  43. the colors are marked as pmExplicit. This tells the palette manager not to
  44. meddle, and to use your arguments to PmForeColor() and PmBackColor() literally.
  45.  
  46. I suggest that you control the clut by calling GDUncorrectedGamma() and
  47. GDSetEntries(), since these calls directly control the video device driver,
  48. bypassing the Color Manager. Since the Color and Palette Managers don't know
  49. that you've changed the color environment they can't react to it, and will
  50. passively let you continue to specify colors by their clut index. For example,
  51. the Palette Manager religiously believes that the first clut entry should be
  52. white and the last one black, and it will change them back to those values if
  53. you change them and it finds out about it. If you use the Color Manager
  54. SetEntries call then the Paletter Manger WILL find out because a record is made
  55. in the ColorTable. Calling GDSetEntries() bypasses the Color Manager, instead
  56. the video device driver writes directly to the clut and the ColorTable is not
  57. modified. Of course, this means that you should ignore the ColorTable since it
  58. will no longer reflect the contents of the clut.
  59.  
  60. The Inside Mac books suggest that a new color window should be given its own
  61. color table. This will happen if you set MAKE_COLOR_TABLE true. However, for my
  62. purposes I think of the window and the screen as essentially the same thing, so
  63. I give the window a handle to the screen's color table, so they share the same
  64. table.
  65.  
  66. Every time you access the stdio package, e.g. printf or getch(), THINK C will
  67. move the Console window to the front, which may obscure your window. You can
  68. bring your window back to the front by calling BringToFront().
  69.  
  70. EXAMPLE:
  71.  
  72. Open your window by saying:
  73.     window=GDOpenWindow1(device);
  74. When you're through with the window, get rid of it by calling:
  75.     GDDisposeWindow1(window);
  76. Besides closing the window and disposing of the allocated memory structures it
  77. restores the device's clut to whatever is in the color table associated with device.
  78.  
  79. HISTORY:
  80.  
  81. 12/88        dgp    wrote it
  82. 8/5/89            added call to GDUncorrectedGamma, so I couldn't forget.
  83. 8/15/89     dgp trivia
  84. 3/20/90        dgp    make compatible with MPW C.
  85. 3/29/90        dgp    changed declared returned type from WindowPtr to CWindowPtr, which
  86.                 is what it's really been all along. Same change to argument of
  87.                 GDDisposeWindow(). The new offscreen GWorld calls for the first time
  88.                 make it easier to honestly declare one's windows as color rather
  89.                 than pretending they're not.
  90. 8/24/91        dgp    Made compatible with THINK C 5.0.
  91. 2/1/92        dgp Made optional the device argument to GDDisposeWindow(). If it's
  92.                 NULL, then it will be determined automatically from window.
  93. 2/3/93        dhb    Extracted AddExplicitPalette from GDOpenWindow.
  94. 2/21/93        dgp    HideMenuBar if window is on main screen.
  95. 2/23/93        dgp AddExplicitPalette() returns immediately unless it receives a color 
  96.                 window.
  97.                 Added GDOpenWindow1() and GDDisposeWindow1(), which both use
  98.                 a WindowPtr, instead of a less convenient CWindowPtr.
  99. 3/5/93        dgp    Added calls to UnclipScreen() and RestoreScreenClipping(), so the window
  100.                 now truly fills the whole screen. Edited GDOpenWindow() for clarity.
  101. 3/7/83        dgp    Added calls to GDSaveGamma(device) and GDRestoreGamma(device).
  102. 4/16/93        dgp    Cosmetic editing.
  103. 5/22/93        dgp    Added RemovePalette(), but didn't test it.
  104. */
  105. #include "VideoToolbox.h"
  106.  
  107. #define MAKE_COLOR_TABLE 0
  108.  
  109. CWindowPtr GDOpenWindow(GDHandle device)
  110. // Obsolete. Use GDOpenWindow1 instead.
  111. {
  112.     return (CWindowPtr)GDOpenWindow1(device);
  113. }
  114.  
  115. void GDDisposeWindow(GDHandle device,CWindowPtr window)
  116. // Obsolete. Use GDDisposeWindow1 instead.
  117. {
  118.     GDDisposeWindow1((WindowPtr) window);
  119. }
  120.  
  121.  
  122. WindowPtr GDOpenWindow1(GDHandle device)
  123. {
  124.     WindowPtr window;
  125.     Rect r;
  126.     GDHandle oldDevice;
  127.  
  128.     if(device==NULL)return NULL;
  129.     r=(*device)->gdRect;        // rect of desired screen in global coordinates
  130.     GDSaveGamma(device);
  131.     GDUncorrectedGamma(device);
  132.     UnclipScreen(device);
  133.     oldDevice=GetGDevice();
  134.     SetGDevice(GetMainDevice());
  135.     window=(WindowPtr)NewCWindow(NULL,&r,"\pHi",TRUE,plainDBox,(WindowPtr) -1L,0,123L);
  136.     SetGDevice(oldDevice);
  137.     AddExplicitPalette(window);
  138.     return window;
  139. }
  140.  
  141. void GDDisposeWindow1(WindowPtr window)
  142. // Dispose of window and palette and restore the clut.
  143. {
  144.     GDHandle device;
  145.     
  146.     if(window==NULL)return;
  147.     device=GetWindowDevice(window);
  148.     DisposePalette(GetPalette(window));
  149.     #if MAKE_COLOR_TABLE
  150.         DisposHandle((Handle)(*((CGrafPtr)window)->portPixMap)->pmTable);
  151.     #endif
  152.     DisposeWindow(window);
  153.     if(device==NULL)return;
  154.     GDRestoreGamma(device);
  155.     GDRestoreDeviceClut(device);
  156.     RestoreScreenClipping(device);
  157. }
  158.  
  159. void AddExplicitPalette(WindowPtr window)
  160. // Create a palette for the color window and mark all the entries as explicit.
  161. // Copy the entries from the window's device.
  162. {
  163.     GDHandle device;
  164.     CTabHandle cTabHandle;
  165.     PaletteHandle palette;
  166.     int colors,i;
  167.     OSErr error;
  168.  
  169.     if(window==NULL)return;
  170.     if(((CGrafPtr)window)->portVersion>=0) return;    // Not a color window, return.
  171.     device=GetWindowDevice(window);
  172.     if(device==NULL)PrintfExit("AddExplicitPalette: window has no device!\n");
  173.     cTabHandle=(**(**device).gdPMap).pmTable;
  174.     colors=(*cTabHandle)->ctSize+1;
  175.     #if MAKE_COLOR_TABLE
  176.         error=HandToHand((Handle*)&cTabHandle);
  177.         if(error)PrintfExit("AddExplicitPalette: error %d in copying color table\n",error);
  178.         for(i=0;i<colors;i++) (*cTabHandle)->ctTable[i].value=i;
  179.         (*cTabHandle)->ctFlags &= 0x7fff;
  180.         (*cTabHandle)->ctSeed=GetCTSeed();
  181.         (*((CGrafPtr)window)->portPixMap)->pmTable=cTabHandle;
  182.     #endif
  183.     palette=NewPalette(colors,cTabHandle,pmExplicit,0);
  184.     SetPalette(window,palette,0);
  185. }
  186.  
  187. void RemovePalette(WindowPtr window);
  188.  
  189. void RemovePalette(WindowPtr window)
  190. {
  191.     DisposePalette(GetPalette(window));
  192.     SetPalette(window,NULL,0);    // Not sure whether this is necessary or even ok.
  193. }
  194.